#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#define LED_OFF     _IOW('l', 0, int)
#define LED_ON      _IOW('l', 1, int)
#define LED_STAT    _IOWR('l', 2, int)

int main(int argc, char** argv)
{
    if(argc != 3)
    {
        printf("usage: %s <on/off> <1/2/3>\n", argv[0]);
        return -1;
    }

    int fd = open("/dev/leds",O_RDWR);
    int ledQuest;
    if(strcmp(argv[1], "on") == 0){
        ledQuest = LED_ON;
    }else if(strcmp(argv[1], "off") == 0){
        ledQuest = LED_OFF;
    }else{
        printf("usage: %s <on/off> <1/2/3>\n", argv[0]);
        return -1;
    }

    int ledIndex = strtoul(argv[2], NULL, 0);
    if(ledIndex > 3 || ledIndex < 1)
    {
        printf("usage: %s <on/off> <1/2/3>\n", argv[0]);
        return -1;
    }
    ioctl(fd,ledQuest,&ledIndex);
    ioctl(fd,LED_STAT,&ledIndex);
    printf("led%s is %s\n", argv[2], ledIndex?"on":"off");
    close(fd);
}